package com.bobi.tfca.netty;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class NettyChannelMap {
    private static Map<Integer, Channel> map = new ConcurrentHashMap<>();

    public static void add(Integer memberId, Channel channel) {
        log.info(memberId + " join");
        map.put(memberId, channel);
    }

    public static Map<Integer, Channel> getMap() {
        return map;
    }

    public static Channel get(Integer memberId) {
        return map.get(memberId);
    }

    public static void remove(Channel channel) {
        for (Map.Entry entry : map.entrySet()) {
            if (entry.getValue() == channel) {
                Integer memberId = (Integer) entry.getKey();
                map.remove(memberId);
                log.info(memberId + " leave");
                break;
            }
        }
    }
}